<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/3
 * Time: 13:53
 */

namespace app\api\controller;


use app\common\controller\Base;
use think\Db;

class Lovelist extends Base
{

    /**
     * 申请我帮帮
     * */
    public function apply()
    {

        $type = input('type');
        $token = input('token');

        $realname = input('realname','');
        $sex = input('sex','');
        $danwei = input('danwei','');
        $info = input('info','');
        $rudang_date = input('rudang_date','');
        $mobile = input('mobile','');
        $address = input('address','');
        $addressInfo = input('address_info','');

        $tokenInfo = $this->check($token);
        $user_id = $tokenInfo['uid'];

        $id = input('id','');

        if(!$id){
            $map = [];
            $map[] = ['user_id','=',$user_id];
            $map[] = ['status','>=',0];
            $lastYear = strtotime("-1 year");
            $nextYear = strtotime("+1 year");
            $map[] = ['create_time','between',[$lastYear,$nextYear]];

            $applyCount = Db::name('love_list')->where($map)->count();

            if($applyCount >= 2){
                $this->result('','-1','一年内最多申请两次帮帮','json',[]);
            }
        }

        if($type == 1){

            $wish_info = input('wish_info');
            $channel = input('channel');
            $need_type = input('need_type');

                if(!$realname || !$sex || !$danwei || !$rudang_date || !$mobile || !$channel || !$need_type || !$wish_info || !$address || !$addressInfo){
                    $this->result('','-1','数据填写不完整','json');
                }
                $data = array(
                    'user_id' => $user_id,
                    'type' => $type,
                    'realname' => $realname,
                    'sex' => $sex,
                    'danwei' => $danwei,
                    'rudang_date' => $rudang_date,
                    'mobile' => $mobile,
                    'channel' => $channel,
                    'need_type' => $need_type,
                    'info' =>$info,
                    'wish_info' =>$wish_info,
                    'address' => $address,
                    'address_info' => $addressInfo
                );

                $msg = "申请成功,请耐心等待审核";

        }else{

            $help_political = input('help_political','');
            $help_political_info = input('help_political_info','无');

            if(!$realname || !$sex || !$danwei || !$rudang_date || !$mobile || !$help_political || !$help_political_info){
                    $this->result('','-1','数据填写不完整','json');
                }

                $data = array(
                    'help_id' => $user_id,
                    'type' => $type,
                    'help_name' => $realname,
                    'help_sex' => $sex,
                    'help_danwei' => $danwei,
                    'help_mobile' => $mobile,
                    'help_bron' => $rudang_date,
                    'help_political' => $help_political,
                    'help_political_info' => $help_political_info,
                    'help_info' =>$info,
                    'address_help' => $address,
                    'address_help_info' => $addressInfo
                );

            $msg = "恭喜你成为第0000".$user_id."号志愿者";

            }

        if($id){

            $oldStatus = Db::name('love_list')->where(['id'=>$id])->value('status');
            if($oldStatus == -1){

                $map = [];
                $map[] = ['user_id','=',$user_id];
                $map[] = ['status','>=',0];
                $lastYear = strtotime("-1 year");
                $nextYear = strtotime("+1 year");
                $map[] = ['create_time','between',[$lastYear,$nextYear]];

                $applyCount = Db::name('love_list')->where($map)->count();

                if($applyCount >= 2){
                    $this->result('','-1','一年内最多申请两次帮帮','json',[]);
                }

                $data['status'] = 0;
            }

            $res = Db::name('love_list')->where(['id' => $id])->update($data);
        }else{
            $data['status'] = 0;
            $data['create_time'] = time();
            $res = Db::name('love_list')->insert($data);
        }

        if($res){
            $this->result('','1',$msg,'json');
        }else{
            $this->result('','-1','申请失败','json');
        }

    }


    /**
     * 我帮助页面
     * */
    public function iHelp()
    {

        $political = Db::name('political')->where(['pid' => 0])->field('id,value')->select();
        foreach ($political as $k => $v){
            $politicalInfo = Db::name('political')->where(['pid' => $v['id']])->field('id,value')->select();
            $political[$k]['info'] = $politicalInfo;
        }
        $pro = Db::name('pro')->field('id,value')->order("sort desc")->select();

        foreach ($pro as $k => $v ){

            $arr = Db::name('city')->where(['pid' => $v['id']])->field('id,value')->order("sort desc")->select();
            if(empty($arr)){
                $arr = [];
                $arr1 = [
                    'id' => 0,
                    'value' => '无'
                ];
                array_push($arr,$arr1);
            }
            $pro[$k]['city'] = $arr;

        }


        $data = [
            'political' => $political,
            'address' => $pro
        ];

        $this->result($data,'1','获取数据','json');

    }


    /**
     * 我的捐助详情
     * */
    public function loveInfo()
    {

        $id = input('id','');
        $token = input('token');
        $user = $this->check($token);

        if(!$id){
            $this->result('','-1','数据获取错误','json');
        }
        $info = Db::name('love_list')->where(['id' => $id])->field('sex,create_time,realname,mobile,danwei,info,wish_info,status,help_id,address,address_info')
            ->withAttr('create_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d H:i:s', $value);
                }else{
                    return "--";
                }
            })
            ->find();

        if($info['status'] == 4 && $info['help_id'] == $user['uid']){
            $info['is_zhengshu'] = 1;
        }else{
            $info['is_zhengshu'] = 0;
        }

        if($info['help_id'] != $user['uid']){
            $dCount = mb_strlen($info['danwei'],"utf-8");
            $aCount = mb_strlen($info['address'],"utf-8");
            $aCount2 = mb_strlen($info['address_info'],"utf-8");
            $info['mobile'] = hidestr($info['mobile'],3,4);
            $info['danwei'] = hidestr($info['danwei'],2,$dCount-2);
            $info['address'] = hidestr($info['address'],2,$aCount-2);
            $info['address_info'] = hidestr($info['address_info'],0,$aCount2);
            $info['is_show'] = 0;

            $info['config_name'] = '';
            $info['config_mobile'] = '';
            $info['config_address'] = '';
        }else{
            $info['is_show'] = 1;

            $where[] = ['id','in',[19,20,21]];
            $config = Db::name('config')->where($where)->field('value')->select();

            $info['config_name'] = $config[0]['value'];
            $info['config_mobile'] = $config[1]['value'];
            $info['config_address'] = $config[2]['value'];
        }

        if(empty($info['address'])){
            $info['address'] = "";
        }

        if(empty($info['address_info'])){
            $info['address_info'] = "";
        }


        $this->result($info,'1','获取成功','json');

    }

    /**
     * 证书查看
     * */
    public function zhengShu()
    {

        $id = input('id','');

        $token = input('token');
        $user = $this->check($token);
        if(!$id){
            $this->result('','-1','数据获取错误','json');
        }

        $info = Db::name('love_list')->where(['id' => $id])->field('help_name,create_time,status')
            ->withAttr('create_time', function ($value, $data) {
                return date('Y-m-d H:i:s', $value);
            })->find();

        if($info['status'] !== 4){
            $this->result('','-1','不得查看','json');
        }
        if(!$info['help_name']){
            $info['help_name'] = Db::name('member')->where(['id' => $user['uid']])->value('username');
        }

        $this->result($info,'1','获取成功','json');

    }


    /**
     * 申请帮助
     * */
    public function help()
    {

        $id = input('id','');
        if(!$id){
            $this->result('','-1','数据获取错误','json');
        }

        $love = Db::name('love_list')->where(['id' => $id])->field('type,user_id,help_id')->find();

        $token = input('token');
        $user = $this->check($token);

        if($user['uid'] == $love['user_id'] && $love['type'] == 1){
            $this->result('','-1','不可认领自己的帮帮~','json');
        }

        if($user['uid'] == $love['help_id'] && $love['type'] == 2){
            $this->result('','-1','不可认领自己的帮帮~','json');
        }

        $name = input('name','');
        $mobile = input('mobile','');
        $danwei = input('danwei','');
        $bron_date = input('bron_date','');
        $help_political = input('help_political','');
        $help_political_info = input('help_political_info','无');
        $sex = input('sex','');

        if(!$name || !$mobile || !$danwei || !$bron_date || !$help_political || !$help_political_info){
            $this->result('','-1','数据获取错误','json');
        }

        if($love['type'] == 1){

            $data = [
                'help_id'=>$user['uid'],
                'help_sex' => $sex,
                'help_name' => $name,
                'help_mobile' => $mobile,
                'help_danwei' => $danwei,
                'help_bron' => $bron_date,
                'help_political' => $help_political,
                'help_political_info' => $help_political_info,
                'take_time' => time(),
                'status' => 2
            ];

        }else{
            $data = [
                'user_id' => $user['uid'],
                'realname' => $name,
                'sex' => $sex,
                'danwei' => $danwei,
                'rudang_date' => $bron_date,
                'mobile' => $mobile,
                'help_political' => $help_political,
                'help_political_info' => $help_political_info,
                'take_time' => time(),
                'status' => 2
            ];
        }

        $res = Db::name('love_list')->where(['id'=>$id])->update($data);

        if($res){
            $this->result('','1','申请成功','json');
        }else{
            $this->result('','-1','申请失败','json');
        }

    }

    public function loveListInfoEdit()
    {

        $id = input('id','');
        $token = input('token');
        $user = $this->check($token);

        if(!$id){
            $this->result('','-1','数据获取错误','json');
        }
        $info = Db::name('love_list')->where(['id' => $id])->field('channel,need_type,sex,create_time,realname,mobile,danwei,info,wish_info,status,help_id,address,address_info,rudang_date')
            ->withAttr('create_time', function ($value, $data) {
                if($value){
                    return date('Y-m-d H:i:s', $value);
                }else{
                    return "--";
                }
            })
            ->find();

        if($info['status'] == 4 && $info['help_id'] == $user['uid']){
            $info['is_zhengshu'] = 1;
        }else{
            $info['is_zhengshu'] = 0;
        }

        if(empty($info['address'])){
            $info['address'] = "";
        }else{
            $addressArr = explode('镇',$info['address']);
            $z = 1;
            if(!isset($addressArr[1])){
                $z = 0;
                $addressArr = explode('道',$info['address']);
                if(isset($addressArr[1])){
                    $z = 2;
                    $addressArr = explode('关',$info['address']);
                }
            }
            $addressArr2 = explode('区',$addressArr[0]);
            if($z == 1){
                $info['address'] = $addressArr2[0]."区/".$addressArr2[1]."镇/".$addressArr[1];
            }else if($z == 2){
                $info['address'] = $addressArr2[0]."区/".$addressArr2[1]."关/".$addressArr[1];
            }else{
                $info['address'] = $addressArr2[0]."区/".$addressArr2[1]."道/".$addressArr[1];
            }
        }

        if(empty($info['address_info'])){
            $info['address_info'] = "";
        }

        $this->result($info,'1','获取成功','json');


    }

}